package com.un.ebs.finance.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.DateUtils;
import com.un.ebs.finance.AccountTypeEnum;
import lombok.Data;

import java.util.Date;

/**
 * @author liubo
 * @email liubocs@163.com
 * @date 2020/9/7 21:35
 */
@Data
public class CertificateModel {

    @JSONField(name = "Model")
    private CertificateDto model;

    public static CertificateModel create(String accountBookId, int attachmentCount, Date date, boolean isExamine) {
        CertificateModel obj = new CertificateModel();
        CertificateDto m = new CertificateDto();
        m.setAccountBookId(FNumberDto.value(accountBookId));
        m.setAttachments(String.valueOf(attachmentCount));
        m.setDate(DateUtils.parseDate(date, "yyyy-MM-dd"));
        m.setVoucherGroupId(FNumberDto.value("PRE001"));
        m.setExamineStatus(String.valueOf(isExamine));
        obj.setModel(m);
        return obj;
    }

    public static CertificateModel create(String id, String accountBookId, int attachmentCount, Date date, boolean isExamine) {
        CertificateModel obj = new CertificateModel();
        CertificateDto m = new CertificateDto();
        m.setId(id);
        m.setAccountBookId(FNumberDto.value(accountBookId));
        m.setAttachments(String.valueOf(attachmentCount));
        m.setDate(DateUtils.parseDate(date, "yyyy-MM-dd"));
        m.setVoucherGroupId(FNumberDto.value("PRE001"));
        m.setExamineStatus(String.valueOf(isExamine));
        obj.setModel(m);
        return obj;
    }

    public CertificateEntityDto addDetail(String accountId, String explanation, double money, AccountTypeEnum type, FDetailDto dim) {
        CertificateEntityDto obj = new CertificateEntityDto();
        obj.setAccountId(FNumberDto.value(accountId));
        obj.setExplanation(explanation);
        obj.setAmountFor(String.valueOf(money));
        obj.setCurrencyId(FNumberDto.value("PRE001"));
        obj.setExchangeRateType(FNumberDto.value("HLTX01_SYS"));
        obj.setExchangeRate("1.0");
        if (type == AccountTypeEnum.debit) {
            obj.setDebit(String.valueOf(money));
        } else if (type == AccountTypeEnum.credit) {
            obj.setCredit(String.valueOf(money));
        } else {
            throw new BusinessException("借贷类型不正确。");
        }
        obj.setDetailId(dim);

        return obj;
    }
}
